<template>
  <div class="rent-table">
    <el-form :model="form">
      <!-- <el-form-item label="数据id" label-width="100px">
        <el-input v-model="form.pid" type="text" class="form-input"/>
      </el-form-item> -->
      <el-form-item label="编码" label-width="100px">
        <el-input v-model="form.code" type="text" class="form-input"/>
      </el-form-item>
      <el-form-item label="名称" label-width="100px">
        <el-input v-model="form.name" type="text" class="form-input"/>
      </el-form-item>
      <el-form-item label="排序码" label-width="100px">
        <el-input v-model="form.codesort" type="text" class="form-input"/>
      </el-form-item>
    </el-form>
    <el-button type="primary" class="confirm-button" @click="handleSubmit">确认</el-button>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { addDictionary, checkCode, deleteType } from '@/api/data'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      form: {
        pid: '',
        code: '',
        name: '',
        codesort: ''
      }
    }
  },
  created() {
    this.deleteItem()
  },
  methods: {
    handleSubmit() {
      const flag = this.check()
      if (flag) {
        this.checkData()
      }
    },
    check() {
      const { name, code } = this.form
      if (name.trim() === '') {
        this.$message.warning({
          duration: 1000,
          message: '名称不能为空'
        })
        return false
      }
      if (code.trim() === '') {
        this.$message.warning({
          duration: 1000,
          message: '编码不能为空且要唯一'
        })
        return false
      }
      return true
    },
    checkData() {
      const { code } = this.form
      checkCode(code)
        .then((res) => {
          if (res.success) {
            this.addData()
          } else {
            this.$message.warning({
              duration: 1000,
              message: res.msg
            })
            return
          }
        }).catch((res) => {
          this.$message.warning({
            duration: 1000,
            message: '请稍后再试'
          })
        })
    },
    addData() {
      const { pid, name, code, codesort } = this.form
      addDictionary(pid, code, name, codesort)
        .then((res) => {
          res = res.data
          this.$message.success({
            duration: 1000,
            message: '添加成功'
          })
        }).catch((res) => {
          res = res.data
          this.$message.warning({
            duration: 1000,
            message: res.msg
          })
        })
    },
    deleteItem() {
      deleteType('b2e1dff640874691a97ed50dce492c1f')
        .then((res) => {
          if (res.success) {
            this.$message.success({
              duration: 1000,
              message: '删除成功'
            })
          }
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.rent-table {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  .options-header {
    display: flex;
    margin-bottom: 20px;
    .search-input {
      margin-right: 20px;
      flex:1;
    }
    .select {
      margin-right: 20px;
      flex:1;
    }
    .search-btn {
     flex:1;
    }
  }
  .add-input {
    width: 200px;
  }
  .data-table{
    margin-bottom: 20px;
  }
  .form-input {
    width: 200px;
  }
  .confirm-button {
    width: 200px;
    margin-left: 100px;
  }
}
</style>
